package com.geekaca.news.controller.admin;

import com.geekaca.news.domain.NewsCategory;
import com.geekaca.news.service.CategoryService;
import com.geekaca.news.utils.NewsConstants;
import com.geekaca.news.utils.PageResult;
import com.geekaca.news.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;


    @RequestMapping("/categories")
    public String category(HttpServletRequest request) {
        request.setAttribute("path", "categories");
        return "admin/category";
    }

    @GetMapping("/categories/list")
    @ResponseBody
    public Result categoryList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {

        if (page == null) {
            page = 1;
        }
        if (limit == null) {
            limit = 10;
        }

        //查询记录条数
        Integer allCategoriesCount = categoryService.getTotalCategories();
        List<NewsCategory> categoryList = categoryService.getCategoryList(page, limit);

        Result rs = new Result();
        rs.setResultCode(NewsConstants.RESULT_OK);
        PageResult pageResult = new PageResult(categoryList, allCategoriesCount, limit, page);

        rs.setData(pageResult);
        return rs;
    }

    @PostMapping("/categories/update")
    @ResponseBody
    public Result updateCategory(@RequestParam("categoryId") Integer categoryId,
                                 @RequestParam("categoryName") String categoryName,
                                 @RequestParam("categoryIcon") String categoryIcon) {

        System.out.println("categoryId: " + categoryId);
        Result result = new Result();
        int updated = categoryService.updateCategory(categoryId, categoryName, categoryIcon);
        if (updated > 0) {
            result.setResultCode(NewsConstants.RESULT_OK);
            result.setMessage("更新成功 ");
        } else {
            result.setResultCode(NewsConstants.RESULT_FAIL);
            result.setMessage("更新失败");
        }
        return result;
    }

    @PostMapping("/categories/save")
    @ResponseBody
    public Result insertCategory(@RequestParam("categoryName") String categoryName,
                                 @RequestParam("categoryIcon") String categoryIcon) {
        Result result = new Result();
        NewsCategory category = new NewsCategory();
        category.setCategoryName(categoryName);
        category.setCategoryIcon(categoryIcon);
        category.setCategoryRank(1);
        category.setIsDeleted(0);
        category.setCreateTime(new Date());
        int i = categoryService.insertCategory(category);
        if (i > 0) {
            result.setResultCode(NewsConstants.RESULT_OK);
            result.setMessage("新增成功");
        } else {
            result.setResultCode(NewsConstants.RESULT_FAIL);
            result.setMessage("新增失败");
        }
        return result;
    }

    @PostMapping("/categories/delete")
    @ResponseBody
    public Result deleteCategory(@RequestBody List<Integer> catIds) {
        System.out.println("catgory ids: " + catIds);
        Result result = new Result();
        catIds.forEach(catId -> {
            int deleted = categoryService.deleteCategory(catId);
            if (deleted > 0) {
                result.setResultCode(NewsConstants.RESULT_OK);
                result.setMessage("删除成功 ");
            } else {
                result.setResultCode(NewsConstants.RESULT_FAIL);
                result.setMessage("删除失败");
            }
        });
        return result;
    }
}
